package com.woniuxy.woniuticket.movieinfomodule.service.impl;

import com.woniuxy.woniuticket.movieinfomodule.dao.CinemaVipDAO;
import com.woniuxy.woniuticket.movieinfomodule.dao.UserVipInfoDAO;
import com.woniuxy.woniuticket.movieinfomodule.dao.UsersInfoDAO;
import com.woniuxy.woniuticket.movieinfomodule.dto.LoginUserAndVip;
import com.woniuxy.woniuticket.movieinfomodule.dto.UserandVip;
import com.woniuxy.woniuticket.movieinfomodule.service.UserVipInfoService;
import com.woniuxy.woniuticket.pojo.UserVipInfo;
import com.woniuxy.woniuticket.pojo.UsersInfo;
import com.woniuxy.woniuticket.pojo.UsersLogin;
import com.woniuxy.woniuticket.usermodule.dao.UsersLoginDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service
public class UserVipInfoServiceImp implements UserVipInfoService {

    @Resource
    private UserVipInfoDAO userVipInfoDAO;
    @Resource
    private UsersInfoDAO usersInfoDAO;
    @Resource
    private UsersLoginDAO loginDAO;
    @Resource
    private CinemaVipDAO cinemaVipDAO;

    //开通Vip
    @Override
    public String openVip(UserandVip userandVip) {
        //查询该用户是否存在
        UsersLogin usersLogin = new UsersLogin();
        usersLogin.setAccount(userandVip.getTelnumber());
        UsersLogin usersLogin1 = loginDAO.selectUserByAccount(usersLogin);

        Integer  vipId = cinemaVipDAO.queryVipId(userandVip.getVname());

        if(vipId== null || vipId == 0){
            return "该VIP类型不存在";
        }

        if(usersLogin1 == null){//不存在该用户
            return "用户不存在";
        }else{//存在该用户

            UserVipInfo userVipInfo = new UserVipInfo();
            userVipInfo.setStartDate(userandVip.getStartDate());
            userVipInfo.setVipId(vipId);
            userVipInfo.setUserId(usersLogin1.getUserInfoId());

            //验证当前用户是否已经办理VIP
            UserVipInfo user = userVipInfoDAO.querySingleVipUser(usersLogin1.getUserInfoId());

            if(user == null){

                userVipInfoDAO.insertUserVipInfo(userVipInfo);
                return "开通VIP成功";
            }else {
                long time = user.getEndDate().getTime();
                long time2 = new  Date().getTime();
                if(time<time2) {
                    System.out.println(user);
                    //先删除用户
                    userVipInfoDAO.deleUserVipInfo(user.getUserId());
                    //在插入
                    userVipInfoDAO.insertUserVipInfo(userVipInfo);
                    return "已修改套餐";
                } else{
                    return "用户已办理VIP";
                }
            }
        }
    }

    public String changeVip(UserVipInfo userVipInfo){


        //验证当前用户是否已经办理VIP
        UserVipInfo user = userVipInfoDAO.querySingleVipUser(userVipInfo.getUserId());

        if(user == null){
            userVipInfoDAO.insertUserVipInfo(userVipInfo);
            return "开通VIP成功";
        }else {
            long time = user.getEndDate().getTime();
            long time2 = new  Date().getTime();
            if(time<time2) {
                System.out.println(user);
                //先删除用户
                userVipInfoDAO.deleUserVipInfo(user.getUserId());
                //在插入
                userVipInfoDAO.insertUserVipInfo(userVipInfo);
                return "已修改套餐";
            } else{
                return "用户已办理VIP";
            }
        }
    }

    //删除用户VIP信息
    @Override
    public String deleUserVipInfo(Integer userId) {
        UserVipInfo  vipUser = userVipInfoDAO.querySingleVipUser(userId);
        if(vipUser != null){
            userVipInfoDAO.deleUserVipInfo(userId);
            return "success";
        }else{
            return "fail";
        }

    }

    @Override
    public void deledeleUserVipInfoForDateout(Date date) {
        userVipInfoDAO.deledeleUserVipInfoForDateout(date);
    }

    @Override
    public List<UserVipInfo> queryUserVipInfo(Date date) {
        return userVipInfoDAO.queryUserVipInfo(date);
    }

    @Override
    public int queryUserNum(Integer vipId) {
        return userVipInfoDAO.queryUserNum(vipId);
    }

    @Override
    public List<UserandVip> queryAll() {
        return userVipInfoDAO.queryAll();
    }

    @Override
    public LoginUserAndVip queryAllForLogin(String account) {
        return userVipInfoDAO.queryAllForLogin(account);
    }

    @Override
    public List<UserandVip> queryAll1(UserandVip userandVip) {
        return userVipInfoDAO.queryAll1(userandVip);
    }

    //获取用户信息
    @Override
    public UserandVip queryUserInfo(String telnumber) {
        return userVipInfoDAO.queryUserInfo(telnumber);
    }

    @Override
    public void insertUserVipInfo(UserVipInfo userVipInfo) {
        userVipInfoDAO.insertUserVipInfo(userVipInfo);
    }


}
